package com.gscitysfy.cus.drain.modules.duty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gscitysfy.cloudfrmwk.data.mybatis.HerodotusIdentifierGenerator;
import com.gscitysfy.cloudfrmwk.security.definition.core.HerodotusUserDetails;
import com.gscitysfy.cloudfrmwk.security.utils.SecurityUtils;

import com.gscitysfy.cus.drain.modules.duty.entity.MoperDutyMnRule;
import com.gscitysfy.cus.drain.modules.duty.mapper.MoperDutyMnRuleMapper;
import com.gscitysfy.cus.drain.modules.duty.service.IMoperDutyMnRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.List;


/**
 * @author 刘少华
 * @version 1.0
 * @description: 排班规则接口实现类
 * @date 2022/3/28 16:07
 */
@Service
@Slf4j
public class MoperDutyMnRuleServiceImpl extends ServiceImpl<MoperDutyMnRuleMapper, MoperDutyMnRule> implements IMoperDutyMnRuleService {

    @Autowired
    private MoperDutyMnRuleMapper dutyRuleMapper;
    @Autowired
    private HerodotusIdentifierGenerator herodotusIdentifierGenerator;

    /**
     * 保存排班规则
     *
     * @param listRule 排班规则实体集合
     * @return 是否保存成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDutyRule(List<MoperDutyMnRule> listRule) {
        try {
            //先将正在生效的规则设为失效
            UpdateWrapper<MoperDutyMnRule> wrapper = new UpdateWrapper<>();
            wrapper.eq("coam_usable", 0).set("coam_usable", 1);
            this.update(null, wrapper);
            String personId = "";
            HerodotusUserDetails principal = SecurityUtils.getPrincipal();
            if (principal != null) {
                personId = principal.getUserId();
            }
            for (MoperDutyMnRule dutyMnRule : listRule) {
                //新增生效的规则
                dutyMnRule.setCoamKid(herodotusIdentifierGenerator.nextId(dutyMnRule).toString());
                dutyMnRule.setCoamAddtime(new Timestamp(System.currentTimeMillis()));
                dutyMnRule.setCoamUptime(new Timestamp(System.currentTimeMillis()));
                dutyMnRule.setCoamCreator(personId);
                dutyMnRule.setCoamModifier(personId);
                dutyMnRule.setCoamUsable(0L);
            }
            this.saveBatch(listRule);
            return true;
        } catch (Exception e) {
            //捕捉失败异常并回滚，返回false
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("save duty rule error!");
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询生效的排班规则
     *
     * @return 排班规则实体集合
     */
    @Override
    public List<MoperDutyMnRule> getDutyRule() {
        //查询条件“coam_usable=0”表示唯一生效
        return dutyRuleMapper.selectList(
                new LambdaQueryWrapper<MoperDutyMnRule>()
                        .eq(MoperDutyMnRule::getCoamUsable, 0)
        );
    }

}
